package online.westonline.com.westonline.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.nekocode.badge.BadgeDrawable;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.utils.UiUtlis;

/**
 * 选图预览适配器
 * Created by xiatao on 2018/6/14.
 */

public class MyPerviewAdpter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<String> paths = new ArrayList<>();
    private clicktodelte clicktodelte;
    private clicktoadd clicktoadd;
    //预留的+号
    private int BOTTOM_VIEW=1;
    public void setPaths(List<String> paths) {
        this.paths = paths;
    }

    public void setClicktoadd(MyPerviewAdpter.clicktoadd clicktoadd) {
        this.clicktoadd = clicktoadd;
    }

    public void setClicktodelte(MyPerviewAdpter.clicktodelte clicktodelte) {
        this.clicktodelte = clicktodelte;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        {
             view = LayoutInflater.from(parent.getContext()).inflate(R.layout.periview_item, parent, false);
            return new MyPerviewViewHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyPerviewViewHolder)
        {
            MyPerviewViewHolder holder1 = (MyPerviewViewHolder) holder;
            Glide.with(holder1.itemView.getContext()).load(new File(paths.get(position))).into(holder1.imgPreiview);
            BadgeDrawable drawable = new BadgeDrawable.Builder()
                    .type(BadgeDrawable.TYPE_ONLY_ONE_TEXT)
                    .badgeColor(UiUtlis.getColor(R.color.TopicItemNodeColor))
                    .textSize(15)
                    .text1("取消")
                    .textColor(UiUtlis.getColor(R.color.colorAppTheme))
                    .build();
            holder1.imgBage.setImageDrawable(drawable);
        }


    }



    @Override
    public int getItemCount() {
        return paths == null ? 0 : paths.size();
    }

    class MyPerviewViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_bage)
        ImageView imgBage;
        @BindView(R.id.img_preiview)
        ImageView imgPreiview;
        public MyPerviewViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clicktodelte!=null)
                    {
                        clicktodelte.delete(getLayoutPosition(),paths.get(getLayoutPosition()));
                    }
                }
            });
        }
    }

    /**
     * 添加按钮
     */
    public class MyAddViewHolder extends RecyclerView.ViewHolder {
        public MyAddViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clicktoadd!=null)
                    {
                        clicktoadd.delete();
                    }
                }
            });
        }
    }
    public interface clicktodelte
    {
        void delete(int pos, String path);
    }
    public interface clicktoadd
    {
        void delete();
    }
}
